package indi.zhifa.core.dataauth.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import indi.zhifa.core.dataauth.handler.ZfDataPermissionInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Configuration
public class MybatisPlusInterceptorEnhancer implements SmartInitializingSingleton {

    private final MybatisPlusInterceptor mMybatisPlusInterceptor;
    private final ZfDataPermissionInterceptor mZfDataPermissionInterceptor;

    @Override
    public void afterSingletonsInstantiated() {
        List<InnerInterceptor> orgInnerInterceptor = mMybatisPlusInterceptor.getInterceptors();
        List<InnerInterceptor> newInnerInterceptor = new ArrayList<>();
        newInnerInterceptor.add(mZfDataPermissionInterceptor);
        newInnerInterceptor.addAll(orgInnerInterceptor);
        mMybatisPlusInterceptor.setInterceptors(newInnerInterceptor);
    }
}
